#include  	"main.h"
#include 	"system.h"

/***
����һ��������Ҫ4s
***/
uint8	SendMessageToServer(CollectedData*value)
{
	uint8 i = 0, errcode,j=0;
	uint16 crc;
	
	uint8 sendbuffer[IMEI_Len+ICCID_Len+(37)*2+1] = {
						   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						   36,					
						   0xb1, 0Xb1,		
						   0X01,			 
						   0x00,0x00,0x00,0x00,  
						   0x00,0x00,0x00,0x00, 0x00,0x00,0x00, 
						   0x00,0x00,0x00,0x00, 0x00,0x00,0x00, 
						   0x00,0x00,0x00,0x00, 0x00,0x00,0x00, 
						   0x00,0x00,0x00,	
						   0x00,0x00,		
						   0x00,			
						   0x00, 0x00,		
						   0};
	uint8 databuffer[200]={0};

	

	
    if(SysPara.ChannleStatus.CurChannle_a==Channle_Status_Open) 
    {
		if(value->CurData.aVal>SysPara.thresholdVal.CurThreshold_a)
		{
			sendbuffer[IMEI_Len+ICCID_Len+4]|=Cur_Warn_OverCurrent<<Current_Shift_Val;
		}
		else  if(value->CurData.aVal>Current_Zero_Point_abc)
		{
			sendbuffer[IMEI_Len+ICCID_Len+4]|=Cur_Warn_Normal<<Current_Shift_Val;
			
		}
		else
		{
			sendbuffer[IMEI_Len+ICCID_Len+4]|=Cur_Warn_NoSensor<<Current_Shift_Val;	
		}		

	}
	else
	{
		sendbuffer[IMEI_Len+ICCID_Len+4]|=Cur_Warn_Normal<<Current_Shift_Val;
	}

    if(SysPara.ChannleStatus.VolChannle_a==Channle_Status_Open)
    {
		if(value->VolData.aVal>SysPara.thresholdVal.Vol_OverThreshold_a)
		{
			sendbuffer[IMEI_Len+ICCID_Len+4]|=Vol_Warn_OverVoltage<<Voltage_Shift_Val;
		}
		else	if(value->VolData.aVal>SysPara.thresholdVal.Vol_BelowThreshold_a)
		{
			sendbuffer[IMEI_Len+ICCID_Len+4]|=Vol_Warn_Normal<<Voltage_Shift_Val;
		}
		else	if( value->VolData.aVal>Voltage_Zero_Point)
		{
			sendbuffer[IMEI_Len+ICCID_Len+4]|=Vol_Warn_UnderVoltage<<Voltage_Shift_Val;
		}
		else 
		{
			sendbuffer[IMEI_Len+ICCID_Len+4]|=Vol_Warn_CutOFF<<Voltage_Shift_Val;
		}
	}
	else 
	{
		sendbuffer[IMEI_Len+ICCID_Len+4]|=Vol_Warn_Normal<<Voltage_Shift_Val;
	}

    if(SysPara.ChannleStatus.TemChannle_a==Channle_Status_Open) 
    {
		if( value->TemData.aVal>SysPara.thresholdVal.TemThreshold_a)
		{	
			sendbuffer[IMEI_Len+ICCID_Len+4]|=Tem_Warn_OverHigh<<Temperature_Shift_Val;
		}
		else	if( value->TemData.aVal>0)
		{
			sendbuffer[IMEI_Len+ICCID_Len+4]|=Tem_Warn_Normal<<Temperature_Shift_Val;
		}
		else
		{
			
			sendbuffer[IMEI_Len+ICCID_Len+4]|=Tem_Warn_NoSensor<<Temperature_Shift_Val;
		}

	}
	else
	{
		sendbuffer[IMEI_Len+ICCID_Len+4]|=Tem_Warn_Normal<<Temperature_Shift_Val;
	}




    if(SysPara.ChannleStatus.CurChannle_b==Channle_Status_Open) 
    {
		if(value->CurData.bVal>SysPara.thresholdVal.CurThreshold_b)
		{
			sendbuffer[IMEI_Len+ICCID_Len+5]|=Cur_Warn_OverCurrent<<Current_Shift_Val;
		}
		else  if(value->CurData.bVal>Current_Zero_Point_abc)
		{
			sendbuffer[IMEI_Len+ICCID_Len+5]|=Cur_Warn_Normal<<Current_Shift_Val;
			
		}
		else
		{
			sendbuffer[IMEI_Len+ICCID_Len+5]|=Cur_Warn_NoSensor<<Current_Shift_Val;	
		}		

	}
	else
	{
		sendbuffer[IMEI_Len+ICCID_Len+5]|=Cur_Warn_Normal<<Current_Shift_Val;
	}

    if(SysPara.ChannleStatus.VolChannle_b==Channle_Status_Open)
    {
		if(value->VolData.bVal>SysPara.thresholdVal.Vol_OverThreshold_b)
		{
			sendbuffer[IMEI_Len+ICCID_Len+5]|=Vol_Warn_OverVoltage<<Voltage_Shift_Val;
		}
		else	if(value->VolData.bVal>SysPara.thresholdVal.Vol_BelowThreshold_b)
		{
			sendbuffer[IMEI_Len+ICCID_Len+5]|=Vol_Warn_Normal<<Voltage_Shift_Val;
		}
		else	if( value->VolData.bVal>Voltage_Zero_Point)
		{
			sendbuffer[IMEI_Len+ICCID_Len+5]|=Vol_Warn_UnderVoltage<<Voltage_Shift_Val;
		}
		else 
		{
			sendbuffer[IMEI_Len+ICCID_Len+5]|=Vol_Warn_CutOFF<<Voltage_Shift_Val;
		}
	}
	else 
	{
		sendbuffer[IMEI_Len+ICCID_Len+5]|=Vol_Warn_Normal<<Voltage_Shift_Val;
	}

    if(SysPara.ChannleStatus.TemChannle_b==Channle_Status_Open) 
    {
		if( value->TemData.bVal>SysPara.thresholdVal.TemThreshold_b)
		{	
			sendbuffer[IMEI_Len+ICCID_Len+5]|=Tem_Warn_OverHigh<<Temperature_Shift_Val;
		}
		else	if( value->TemData.bVal>0)
		{
			sendbuffer[IMEI_Len+ICCID_Len+5]|=Tem_Warn_Normal<<Temperature_Shift_Val;
		}
		else
		{
			
			sendbuffer[IMEI_Len+ICCID_Len+5]|=Tem_Warn_NoSensor<<Temperature_Shift_Val;
		}

	}
	else
	{
		sendbuffer[IMEI_Len+ICCID_Len+5]|=Tem_Warn_Normal<<Temperature_Shift_Val;
	}



    if(SysPara.ChannleStatus.CurChannle_c==Channle_Status_Open) 
    {
		if(value->CurData.cVal>SysPara.thresholdVal.CurThreshold_c)
		{
			sendbuffer[IMEI_Len+ICCID_Len+6]|=Cur_Warn_OverCurrent<<Current_Shift_Val;
		}
		else  if(value->CurData.cVal>Current_Zero_Point_abc)
		{
			sendbuffer[IMEI_Len+ICCID_Len+6]|=Cur_Warn_Normal<<Current_Shift_Val;
			
		}
		else
		{
			sendbuffer[IMEI_Len+ICCID_Len+6]|=Cur_Warn_NoSensor<<Current_Shift_Val;	
		}		

	}
	else
	{
		sendbuffer[IMEI_Len+ICCID_Len+6]|=Cur_Warn_Normal<<Current_Shift_Val;
	}

    if(SysPara.ChannleStatus.VolChannle_c==Channle_Status_Open)
    {
		if(value->VolData.cVal>SysPara.thresholdVal.Vol_OverThreshold_c)
		{
			sendbuffer[IMEI_Len+ICCID_Len+6]|=Vol_Warn_OverVoltage<<Voltage_Shift_Val;
		}
		else	if(value->VolData.cVal>SysPara.thresholdVal.Vol_BelowThreshold_c)
		{
			sendbuffer[IMEI_Len+ICCID_Len+6]|=Vol_Warn_Normal<<Voltage_Shift_Val;
		}
		else	if( value->VolData.cVal>Voltage_Zero_Point)
		{
			sendbuffer[IMEI_Len+ICCID_Len+6]|=Vol_Warn_UnderVoltage<<Voltage_Shift_Val;
		}
		else 
		{
			sendbuffer[IMEI_Len+ICCID_Len+6]|=Vol_Warn_CutOFF<<Voltage_Shift_Val;
		}
	}
	else 
	{
		sendbuffer[IMEI_Len+ICCID_Len+6]|=Vol_Warn_Normal<<Voltage_Shift_Val;
	}

    if(SysPara.ChannleStatus.TemChannle_c==Channle_Status_Open) 
    {
		if( value->TemData.cVal>SysPara.thresholdVal.TemThreshold_c)
		{	
			sendbuffer[IMEI_Len+ICCID_Len+6]|=Tem_Warn_OverHigh<<Temperature_Shift_Val;
		}
		else	if( value->TemData.cVal>0)
		{
			sendbuffer[IMEI_Len+ICCID_Len+6]|=Tem_Warn_Normal<<Temperature_Shift_Val;
		}
		else
		{
			
			sendbuffer[IMEI_Len+ICCID_Len+6]|=Tem_Warn_NoSensor<<Temperature_Shift_Val;
		}

	}
	else
	{
		sendbuffer[IMEI_Len+ICCID_Len+6]|=Tem_Warn_Normal<<Temperature_Shift_Val;
	}

 

    if(SysPara.ChannleStatus.TemChannle_s==Channle_Status_Open) 
    {
		if(value->TemData.sVal>SysPara.thresholdVal.TemThreshold_s)
		{
			
			sendbuffer[IMEI_Len+ICCID_Len+7]|=Tem_Warn_OverHigh<<0;
		}
		else	if(value->TemData.sVal>0)
		{
			
			sendbuffer[IMEI_Len+ICCID_Len+7]|=Tem_Warn_Normal<<0;
		}
		else
		{
				
			sendbuffer[IMEI_Len+ICCID_Len+7]|=Tem_Warn_NoSensor<<0;
		}

	}
	else  
	{
		sendbuffer[IMEI_Len+ICCID_Len+7]|=Tem_Warn_Normal<<0;
	}



	

    if(SysPara.ChannleStatus.CurChannle_r==Channle_Status_Open) 
    {
		if(value->CurData.rVal>SysPara.thresholdVal.CurThreshold_r)
		{
			sendbuffer[IMEI_Len+ICCID_Len+7]|=Cur_Warn_OverCurrent<<2;
		}
		else  if(value->CurData.rVal>Current_Zero_Point_r)
		{
			
			sendbuffer[IMEI_Len+ICCID_Len+7]|= Cur_Warn_Normal<<2;
		}
		else
		{
			sendbuffer[IMEI_Len+ICCID_Len+7]|=Cur_Warn_NoSensor<<2;
		}		

	}
	else
	{
		sendbuffer[IMEI_Len+ICCID_Len+7]|=Cur_Warn_Normal<<2;
	}


	sendbuffer[IMEI_Len+ICCID_Len+8]=(uint8)((value->CurData.aVal&0xFF00)>>8);
	sendbuffer[IMEI_Len+ICCID_Len+9]=(uint8)((value->CurData.aVal&0x00FF)>>0);
	
	sendbuffer[IMEI_Len+ICCID_Len+10]=(uint8)((value->VolData.aVal&0xFF00)>>8);
	sendbuffer[IMEI_Len+ICCID_Len+11]=(uint8)((value->VolData.aVal&0x00FF)>>0);
	if(value->TemData.aVal>0)
	{
		sendbuffer[IMEI_Len+ICCID_Len+12]=0;
		sendbuffer[IMEI_Len+ICCID_Len+13]=(uint8)((value->TemData.aVal&0xFF00)>>8);
		sendbuffer[IMEI_Len+ICCID_Len+14]=(uint8)((value->TemData.aVal&0x00FF)>>0);
	}
	else
	{
		sendbuffer[IMEI_Len+ICCID_Len+12]=1;
		sendbuffer[IMEI_Len+ICCID_Len+13]=(uint8)(((-1)*value->TemData.aVal&0xFF00)>>8);
		sendbuffer[IMEI_Len+ICCID_Len+14]=(uint8)(((-1)*value->TemData.aVal&0x00FF)>>0);
	}

	
	sendbuffer[IMEI_Len+ICCID_Len+15]=(uint8)((value->CurData.bVal&0xFF00)>>8);
	sendbuffer[IMEI_Len+ICCID_Len+16]=(uint8)((value->CurData.bVal&0x00FF)>>0);
	
	sendbuffer[IMEI_Len+ICCID_Len+17]=(uint8)((value->VolData.bVal&0xFF00)>>8);
	sendbuffer[IMEI_Len+ICCID_Len+18]=(uint8)((value->VolData.bVal&0x00FF)>>0);
	if(value->TemData.bVal>0)
	{
		sendbuffer[IMEI_Len+ICCID_Len+19]=0;
		sendbuffer[IMEI_Len+ICCID_Len+20]=(uint8)((value->TemData.bVal&0xFF00)>>8);
		sendbuffer[IMEI_Len+ICCID_Len+21]=(uint8)((value->TemData.bVal&0x00FF)>>0);
	}
	else
	{
		sendbuffer[IMEI_Len+ICCID_Len+19]=1;
		sendbuffer[IMEI_Len+ICCID_Len+20]=(uint8)(((-1)*value->TemData.bVal&0xFF00)>>8);
		sendbuffer[IMEI_Len+ICCID_Len+21]=(uint8)(((-1)*value->TemData.bVal&0x00FF)>>0);
	}


	sendbuffer[IMEI_Len+ICCID_Len+22]=(uint8)((value->CurData.cVal&0xFF00)>>8);
	sendbuffer[IMEI_Len+ICCID_Len+23]=(uint8)((value->CurData.cVal&0x00FF)>>0);
	
	sendbuffer[IMEI_Len+ICCID_Len+24]=(uint8)((value->VolData.cVal&0xFF00)>>8);
	sendbuffer[IMEI_Len+ICCID_Len+25]=(uint8)((value->VolData.cVal&0x00FF)>>0);
	if(value->TemData.cVal>0)
	{
		sendbuffer[IMEI_Len+ICCID_Len+26]=0;
		sendbuffer[IMEI_Len+ICCID_Len+27]=(uint8)((value->TemData.cVal&0xFF00)>>8);
		sendbuffer[IMEI_Len+ICCID_Len+28]=(uint8)((value->TemData.cVal&0x00FF)>>0);
	}
	else
	{
		sendbuffer[IMEI_Len+ICCID_Len+26]=1;
		sendbuffer[IMEI_Len+ICCID_Len+27]=(uint8)(((-1)*value->TemData.cVal&0xFF00)>>8);
		sendbuffer[IMEI_Len+ICCID_Len+28]=(uint8)(((-1)*value->TemData.cVal&0x00FF)>>0);
	}



	if(value->TemData.sVal>0)
	{
		sendbuffer[IMEI_Len+ICCID_Len+29]=0;
		sendbuffer[IMEI_Len+ICCID_Len+30]=(uint8)((value->TemData.sVal&0xFF00)>>8);
		sendbuffer[IMEI_Len+ICCID_Len+31]=(uint8)((value->TemData.sVal&0x00FF)>>0);
	}
	else
	{
		sendbuffer[IMEI_Len+ICCID_Len+29]=1;
		sendbuffer[IMEI_Len+ICCID_Len+30]=(uint8)(((-1)*value->TemData.sVal&0xFF00)>>8);
		sendbuffer[IMEI_Len+ICCID_Len+31]=(uint8)(((-1)*value->TemData.sVal&0x00FF)>>0);
	}

	
	sendbuffer[IMEI_Len+ICCID_Len+32]=(uint8)((value->CurData.rVal&0xFF00)>>8);
	sendbuffer[IMEI_Len+ICCID_Len+33]=(uint8)((value->CurData.rVal&0x00FF)>>0);

	
	sendbuffer[IMEI_Len+ICCID_Len+34]=BC26_Check_CESQ();
	
	
	/*crc = SumCalculate(sendbuffer+IMEI_Len+ICCID_Len+1,35);
	sendbuffer[IMEI_Len+ICCID_Len+34] = (crc & 0xFF00) >> 8;
	sendbuffer[IMEI_Len+ICCID_Len+35] = crc & 0x00FF;*/

	j=0;
	for(i=IMEI_Len+ICCID_Len;i<IMEI_Len+ICCID_Len+37;i++)
	{
		sprintf(databuffer+j*2,"%02x",sendbuffer[i]);
		++j;
	}
	
	
	memcpy(sendbuffer,value->BC26_IMEI,IMEI_Len);
	memcpy(sendbuffer+IMEI_Len,value->BC26_ICCID,ICCID_Len);

	memcpy(sendbuffer+IMEI_Len+ICCID_Len,databuffer,37*2);
	sendbuffer[IMEI_Len+ICCID_Len+37*2]=0;
	
	printf("sedbuffer=%s\n",sendbuffer);
	
	i = 0;
	while (++i < 3) 
	{
		errcode = SendDataToTelecomPlatform(sendbuffer,sizeof(sendbuffer)-1);
		if (errcode == TRUE)
		{
			return	SuccessFull;
		}
		else
		{
			
			NB_Delay(2000);
			NB_Delay(2000);
		}
	}
	return	FAIL;
}



uint16	ReceiveMessageFromServer(uint8*recebuffer)
{

	uint8	buffer[ReceiveUdpDataMaxLen]={0},len;

	len=ReceiveMessageFromTelecomPlatform(buffer);
	if(len>0)
	{
		if(strncmp(buffer,collectData.BC26_IMEI,IMEI_Len)==0)	
		{

			if(StringToHex(buffer+IMEI_Len+ICCID_Len+2,len-IMEI_Len-ICCID_Len-2,recebuffer)==FALSE)
			{
				return	0;
			}
			else
			{
				return	(len-IMEI_Len-ICCID_Len-2)/2;
			}

		}
	}
	else
	{

		return	0;
	}
	return	0;

}

#if 0
uint8	SendAckToServer(void)
{
	uint8 i = 0,j=0, errcode;
	uint16	crc=0;
	uint8 sendbuffer[IMEI_Len+ICCID_Len+10*2+1] = {
						  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,	
						  0x09,
						  0xC1, 0XC1,
						  0X05, 
						  0x00,0x00,
						  0x00,0x00,
						  0x00,0x00,
						  0x00};
	uint8 databuffer[50]={0};					  
	extern	uint16	SmokeRedAlarmValue,SmokeBlueAlarmValue;
	memcpy(sendbuffer,BC26_IMEI,IMEI_Len);
	sendbuffer[IMEI_Len+ICCID_Len+3+1]=(SmokeRedAlarmValue&0xFF00)>>8;
	sendbuffer[IMEI_Len+ICCID_Len+4+1]=SmokeRedAlarmValue&0x00FF;
	
	sendbuffer[IMEI_Len+ICCID_Len+5+1]=(SmokeBlueAlarmValue&0xFF00)>>8;
	sendbuffer[IMEI_Len+ICCID_Len+6+1]=SmokeBlueAlarmValue&0x00FF;
	
	crc = SumCalculate(sendbuffer+IMEI_Len+ICCID_Len+1,10-1);
	sendbuffer[IMEI_Len+ICCID_Len+7+1] = (crc & 0xFF00) >> 8;
	sendbuffer[IMEI_Len+ICCID_Len+8+1] = crc & 0x00FF;	



	j=0;
	for(i=IMEI_Len+ICCID_Len;i<IMEI_Len+ICCID_Len+10;i++)
	{
		sprintf(databuffer+j*2,"%02x",sendbuffer[i]);
		++j;
	}
	
	memcpy(sendbuffer,BC26_IMEI,IMEI_Len);
	memcpy(sendbuffer+IMEI_Len,BC26_ICCID,ICCID_Len);
	memcpy(sendbuffer+IMEI_Len+ICCID_Len,databuffer,10*2);
	sendbuffer[IMEI_Len+ICCID_Len+10*2]=0;
	
	printf("sedbuffer=%s\n",sendbuffer);


	
	i = 0;
	while (++i < 3) 
	{
		
		errcode = SendDataToTelecomPlatform(sendbuffer, sizeof(sendbuffer)-1);
		if (errcode == TRUE)
		{
			return	TRUE;

		}
		else
		{
			NB_Delay(1000);
			NB_Delay(1000);
			NB_Delay(1000);
			NB_Delay(1000);
			NB_Delay(1000);
		}
	}

	return	FALSE;


}


void	CMDProcess(	uint8*cmdbuffer,uint8 len)
{
	extern	uint16	BuzzerCmd;
	uint8	i=0;

	if ((SumCheck(cmdbuffer, len) == 1)&&(cmdbuffer[0]==0xc1)&&(cmdbuffer[1]==0xc1))
	{
		printf("bb=%02x\n",cmdbuffer[2]);
		switch(cmdbuffer[2])
		{
			case 0x04:
				printf("--->>red=%d\n",  cmdbuffer[3]<<8|cmdbuffer[4]);
				printf("--->>blue=%d\n", cmdbuffer[5]<<8|cmdbuffer[6]);
				
				
				
				
				SmokeRedAlarmValue   =cmdbuffer[3]<<8|cmdbuffer[4];
				SmokeBlueAlarmValue  =cmdbuffer[5]<<8|cmdbuffer[6];
				SendAckToServer();
				break;
			case 0x03:
				BuzzerCmd=cmdbuffer[3];
				printf("BuzzerCmd=%02x\n",BuzzerCmd);
				break;	

						
			default:
				break;	


		}

	}
	
}
uint8 NBSendGetBuzzerCmd(uint8	socketid)
{
	uint8 i = 0,j=0, errcode;
	uint16	crc = 0;
	uint8 sendbuffer[IMEI_Len+ICCID_Len+6*2+1] = {
						  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
						  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,	
						  0x05,	
						  0xC1, 0XC1,	
						  0X02, 
						  0x00,0x00,
						  0};
	uint8 databuffer[50]={0};		
	
	crc = SumCalculate(sendbuffer+IMEI_Len+ICCID_Len+1,6-1);
	
	sendbuffer[IMEI_Len+ICCID_Len+3+1] = (crc & 0xFF00) >> 8;
	sendbuffer[IMEI_Len+ICCID_Len+4+1] = crc & 0x00FF;	

	j=0;
	for(i=IMEI_Len+ICCID_Len;i<IMEI_Len+ICCID_Len+6;i++)
	{
		sprintf(databuffer+j*2,"%02x",sendbuffer[i]);
		++j;
	}
	
	memcpy(sendbuffer,BC26_IMEI,IMEI_Len);
	memcpy(sendbuffer+IMEI_Len,BC26_ICCID,ICCID_Len);
	memcpy(sendbuffer+IMEI_Len+ICCID_Len,databuffer,6*2);
	sendbuffer[IMEI_Len+ICCID_Len+6*2]=0;
	printf("sedbuffer=%s\n",sendbuffer);

	
	i = 0;
	while (++i < 3) 
	{
		errcode = SendDataToTelecomPlatform(sendbuffer, sizeof(sendbuffer)-1);
		if (errcode == TRUE)
		{
			return	TRUE;

		}
		else
		{
			NB_Delay(1000);
			NB_Delay(1000);
			NB_Delay(1000);
			NB_Delay(1000);
			NB_Delay(1000);
		}
	}

	return	FALSE;						  

}
#endif
